from Config.Contest import Config
import pytest
from Utils.Mysql import Mysql
from Utils.read_data import *

@pytest.fixture
def db():
    database=Mysql('10.59.9.12','root','yidong666','a3mall')
    yield database
    database.close()

class Test_a3mall():
    token = None
    uid = None

    @pytest.fixture(scope="session")
    def api(self):
        return Config()

    # 登录
    @pytest.mark.parametrize('data',read_yaml(r'D:\project_python\Auto_2\Data\data.yaml'))
    def test_01_login(self, api,data,db):
        # 请求体
        login_data = {"username": data['username'], "password": data['password']}
        # 发送post请求
        login = api.post(path='/api/public/login', json=login_data)
        # 断言响应状态码
        assert login.status_code == 200
        # 参数化条件判断
        if data['yxj'] == "P1":
            print(login.json())
            amount = db.select("select amount from mall_users where username ='15523615757'")
            assert str(amount[0][0]) == login.json()['data']['amount']
            Test_a3mall.token = f"Bearer {login.json()['data']['token']}"
        else:
            print(data['exp'])


    # 商品首页
    def test_sy(self,api):
        tou = {"auth-token": Test_a3mall.token}
        sy = api.get(
            path='/api/index/list?code=home&type=h5&page=1',
            headers=tou
        )
        print(sy.json())

    # 商品详情
    def test_xq(self,api):
        tou = {"auth-token": Test_a3mall.token}
        ti ={"id":31}
        xq = api.get(
            path="/api/goods/view?id=31",
            headers=tou,
            data=ti
        )
        print(xq.json())

    # 购物车添加
    def test_addgwc(self,api):
        tou = {"auth-token": Test_a3mall.token}
        ti ={"id":31,"sku_id":"","num":1}
        addgwc = api.post(
            path="/api/cart/add",
            headers=tou,
            json=ti
        )
        print("添加成功：",addgwc.json())
    # 购物车列表
    def test_gwc(self,api):
        tou = {"auth-token":Test_a3mall.token}
        gwc = api.get(
            path="/api/cart?page=1",
            headers=tou
        )
        print(gwc.json())
        Test_a3mall.uid = gwc.json()['data']['list'][0]['id']
        print(Test_a3mall.uid)

    # 商品订单
    def test_dd(self,api):
        tou = {"auth-token": Test_a3mall.token}

        dd = api.get(
            path=f"/api/order/confirm?id={Test_a3mall.uid}&type=cart&shipping_type=1",
            headers=tou,

        )
        print(dd.json())
    # 地址
    def test_dz(self,api):
        tou = {"auth-token":Test_a3mall.token}
        dz = api.get(
            path=f"/api/order/confirm?id={Test_a3mall.uid}&type=cart&shipping_type=1&address_id=58",
            headers=tou,
        )
        print(dz.json())
    # 支付
    def test_zf(self,api):
        tou = {"auth-token":Test_a3mall.token}
        ti = {"id":f"{Test_a3mall.uid}","type":"cart","address_id":58,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=591&type=cart"}
        zf = api.post(
            path="/api/order/create",
            headers=tou,
            json=ti
        )
        print(zf.json())